http = require'socket.http'

function random_word(w_type)
	--print("inside random word")
	--print("w_type : ", w_type)

	local nar_str = ""

	local nf = "txt/nouns.txt"
	local vf = "txt/verbs.txt"
	local ajf = "txt/adj.txt"
	local avf = "txt/adv.txt"

	local nouns = {}
	local verbs = {}
	local adjs = {}
	local advs = {}

	if w_type == "noun" then

		for line in love.filesystem.lines(nf) do

			table.insert(nouns, line)

		end

		return select_words(nouns)

	end

	if w_type == "verb" then

		for line in love.filesystem.lines(vf) do

			table.insert(verbs, line)

		end

		return select_words(verbs)

	end

	if w_type == "adj" then



		for line in love.filesystem.lines(ajf) do

			table.insert(adjs, line)

		end

		return select_words(adjs)

	end

	if w_type == "adv" then

		for line in love.filesystem.lines(avf) do

			table.insert(advs, line)

		end

		return select_words(advs)

	end



end

function select_words(list)

	local r = RNG:random(#list)

	return list[r]


end

function adv_adj_noun_string()

	local strs = {}
	strs[1] = random_word("adv")
	strs[1] = random_word("adj")
	strs[2] = random_word("noun")

	local ns = searchable_string(strs)

	return google_image(ns)


end

function adj_noun_string(meta_tiles)

	--print("in adj_noun_string")
	local strs = {}
	--strs[1] = random_word("adv")
	strs[1] = random_word("adj")
	strs[2] = random_word("noun")

	local ns = searchable_string(strs)

	--print("ns : ", ns)

	return google_image(ns, meta_tiles)


end

function draw_narrative()


	if show_nar_image == true then
		--print("showing nar image")
		love.graphics.draw(g_nar_image, 10, 10, 0,
			16/g_nar_image:getWidth(),
			16/g_nar_image:getHeight())

	end

end

function searchable_string(strs)
	--print("in searchable string")

	local newstring = ""

	for i = 1, #strs do

		if newstring ~= "" then
			newstring = newstring.."+"..strs[i]
		else
			newstring = strs[i]
		end

	end
	--print("new string: ", newstring)

	return newstring


end

function google_string(str)

	local url = "http://google.com/search?q="..str
	local search_results = http.request(url)



end


function google_image(str, meta_tiles)
	print ("in google_image")
	meta_tile_count = meta_tile_count + 1

	local url = "http://google.com/search?q="..str.."&tbm=isch" -- &num=1 for first result
	local search_results = http.request(url)
	local reject_images = {
		"/images/google_favicon_128.png",
		"/images/nav_logo124.png",

	}

	local file_types = {
		"png",
		"jpg",
		"gif",
		"bmp",

	}
	--search_results = search_results:gsub("<[%S>]+",string.upper)
	--[%w_-]-%.png
	local images = {}

	for i = 1, #file_types do
		search_results, result_num = search_results:gsub("[%w_%-:/%.%%%$]+%."..file_types[i], function (s)

			for j = 1, #reject_images do
				if reject_images[j] == s then s = "done" end
			end

			if s ~= "done" then
				--print("accepted s : ", s)

				table.insert(images, s)

			end

		end)
	end


	--print("url:  ", url, "results : ", result_num, "text: ", table.tostring(images))

	--images[1] returns first result
	local picked_image = images[1]
	local meta_name = ""
	meta_tiles[meta_tile_count] = {}
	--print("requesting.. ", picked_image)
	picked_image:gsub("[%w_%-]+%.[%w]+", function(s)
		s = string.sub(s, 1, -5)
		--print("image_name : ", s)
		s = s:gsub("[%d_%-]+", " ")
		s = s:gsub("[%w]+", string.lower)
		meta_name = s
		temp_name = s
		return s

	end)



	--print("image_name :", image_name)
	--print("meta_name : ", meta_name)

	table.insert(meta_tiles[meta_tile_count], meta_name)

	local image_step_one = http.request(picked_image)
	--print("received.. ", image_step_one)

	local i_c = image_step_one:sub(1, 1)

	--print("i c : ", i_c)

	if i_c ~= "<" then
		local image_step_two = love.filesystem.newFileData(image_step_one, '', 'file')
		--print("data type : ", image_step_two:type())
		local image_step_three = love.image.newImageData(image_step_two)
		local image_step_four = love.graphics.newImage(image_step_three)
		local image_booya = image_step_four



		return image_step_four
	else

		print("invalid i_c")

		return green_img
	end



end


